package com.eazy.lkv2.web.controller.traffic;

import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.eazy.lkv2.web.controller.BaseController;
import com.eazy.lkv2.web.entity.City;
import com.eazy.lkv2.web.entity.Line;
import com.eazy.lkv2.web.entity.LineName;
import com.eazy.lkv2.web.service.traffic.CityService;
import com.eazy.lkv2.web.service.traffic.LineNameService;
import com.eazy.lkv2.web.service.traffic.LineService;

/**
 * @author jzx
 * @date 2016/2/18
 * @desc 线路管理
 */
@Controller
@RequestMapping(value = "line")
public class LineController extends BaseController {

	@Autowired
	private CityService cityService;
	@Autowired
	private LineService lineService;
	@Autowired
	private LineNameService lineNameService;
	
	/**
	 * 线路查询列表
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView list(HttpServletRequest request) {
		addObject("cityData", cityService.select());
		addObject("lineData", lineService.select());
		return getSuccess("traffic/line/line_list");
	}
	
	/**
	 * 添加线路
	 */
	@RequestMapping(value = "/addLine", method = RequestMethod.POST)
	public String addLine(Line line) {
		lineService.create(line);
		return "redirect:/line/list";
	}
	
	/**
	 * 修改线路
	 */
	@RequestMapping(value = "/updateLine", method = RequestMethod.POST)
	public String updateLine(Line line) {
		lineService.update(line);
		return "redirect:/line/list";
	}
	
	/**
	 * 删除线路
	 */
	@RequiresPermissions("sys:line:delete")
	@RequestMapping(value = "/deleteLine/{id}", method = RequestMethod.GET)
	public String deleteLine(@PathVariable String id) {
		lineService.removeById(id);
		return "redirect:/line/list";
	}
	
	/**
	 * 跳转到添加线路
	 */
	@RequiresPermissions("sys:line:add")
	@RequestMapping(value = "/toAddLine", method = RequestMethod.GET)
	public ModelAndView toAddLine() {
		addObject("cityData", cityService.select());
		return getSuccess("traffic/line/line_add");
	}
	
	/**
	 * 跳转到修改线路页面
	 */
	@RequestMapping(value = "/toUpdateLine/{id}", method = RequestMethod.GET)
	public ModelAndView toUpdateLine(@PathVariable String id) {
		LineName lineModel = lineNameService.findById(parseInt(id));
		City cityModel = cityService.findById(lineModel.getCityId());
		List<Map<String, Object>> cityArray = cityService.select();
		ListIterator<Map<String,Object>> data = cityArray.listIterator();
		synchronized(data) {
			while(data.hasNext()) {
				Map<String,Object> d = data.next();
				if(cityModel.getId() == d.get("id")) {
					data.remove();
				}
			}
		}
		cityArray.add(0, convertObj2Map(cityModel));
		addObject("cityData", cityArray);
		addObject("line_name",lineModel.getName());
		addObject("id", lineModel.getId());
		return getSuccess("traffic/line/line_update");
	}
}
